package com.sddongyun.config;

import com.sddongyun.common.JaxWsDynamicClientFactory;
import com.sddongyun.proxy.CxfProxyServlet;
import com.sddongyun.proxy.HisQueryProxyServlet;
import com.sddongyun.proxy.YbHttpProxyServlet;
import com.sddongyun.proxy.YbProxyServlet;
import org.mitre.dsmiley.httpproxy.ProxyServlet;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.EnvironmentAware;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;

/**
 * Created by lenovo on 2019/4/18.
 */
@Configuration
public class ProxConfig implements EnvironmentAware {

    private static class SingletonJaxWsDynamicClientFactory {
        private static final JaxWsDynamicClientFactory INSTANCE =  JaxWsDynamicClientFactory.newInstance();
    }

    public static JaxWsDynamicClientFactory getJaxWsDynamicClientFactory() {
        return SingletonJaxWsDynamicClientFactory.INSTANCE;
    }
//    @Bean
//    public ServletRegistrationBean servletRegistrationBean10(){
//        ServletRegistrationBean servletRegistrationBean =
//                new ServletRegistrationBean(new proxyServlet(),"/jmjkk/api/sign");
//        //这个setName必须要设置，并且多个的时候，名字需要不一样
//        servletRegistrationBean.setName("jmjkk_api_sign");
//
//        servletRegistrationBean.setLoadOnStartup(1);
//        servletRegistrationBean.addInitParameter("targetUri", "http://172.16.96.16:8098/api/sign");
//        servletRegistrationBean.addInitParameter(ProxyServlet.P_LOG, "false");
//
//        return servletRegistrationBean;
//    }
//    @Bean
//    public ServletRegistrationBean servletRegistrationBean9(){
//        ServletRegistrationBean servletRegistrationBean =
//                new ServletRegistrationBean(new proxyServlet(),"/jmjkk/api/deciphering");
//        //这个setName必须要设置，并且多个的时候，名字需要不一样
//        servletRegistrationBean.setName("jmjkk_api_deciphering");
//
//        servletRegistrationBean.setLoadOnStartup(1);
//        servletRegistrationBean.addInitParameter("targetUri", "http://172.16.96.16:8098/api/deciphering");
//        servletRegistrationBean.addInitParameter(ProxyServlet.P_LOG, "false");
//
//        return servletRegistrationBean;
//    }
//    @Bean
//    public ServletRegistrationBean servletRegistrationBean8(){
//        ServletRegistrationBean servletRegistrationBean =
//                new ServletRegistrationBean(new proxyServlet(),"/jmjkk/api/encrypt");
//        //这个setName必须要设置，并且多个的时候，名字需要不一样
//        servletRegistrationBean.setName("jmjkk_api_encrypt");
//
//        servletRegistrationBean.setLoadOnStartup(1);
//        servletRegistrationBean.addInitParameter("targetUri", "http://172.16.96.16:8098/api/encrypt");
//        servletRegistrationBean.addInitParameter(ProxyServlet.P_LOG, "false");
//
//        return servletRegistrationBean;
//    }

    /**
     * 此方法不能代理居民健康卡平台，因初始化时，连接平台地址报错
     * 修改为：在nginx中代理
     * @return
     */
//    @Bean
    public ServletRegistrationBean servletRegistrationBean7(){
        ServletRegistrationBean servletRegistrationBean =
                new ServletRegistrationBean(new CxfProxyServlet(),"/jmjkk/api/*");
        //这个setName必须要设置，并且多个的时候，名字需要不一样
        servletRegistrationBean.setName("jmjkk_api");

        servletRegistrationBean.setLoadOnStartup(1);
        servletRegistrationBean.addInitParameter("targetUri", "http://172.16.96.16:8098/api");
        servletRegistrationBean.addInitParameter(ProxyServlet.P_LOG, "true");

        return servletRegistrationBean;
    }
    @Bean
    public ServletRegistrationBean servletRegistrationBean6(){
        ServletRegistrationBean servletRegistrationBean = new ServletRegistrationBean(new CxfProxyServlet(),"/img/*");
        //这个setName必须要设置，并且多个的时候，名字需要不一样
        servletRegistrationBean.setName("img");
        servletRegistrationBean.addInitParameter("targetUri", "https://pay.swiftpass.cn");
        servletRegistrationBean.addInitParameter(ProxyServlet.P_LOG, "true");
        return servletRegistrationBean;
    }
    @Bean
    public ServletRegistrationBean servletRegistrationBean3(){
        ServletRegistrationBean servletRegistrationBean = new ServletRegistrationBean(new CxfProxyServlet(),"/qrpay/*");
        //这个setName必须要设置，并且多个的时候，名字需要不一样
        servletRegistrationBean.setName("qrpay");
        servletRegistrationBean.setLoadOnStartup(1);
        servletRegistrationBean.addInitParameter("targetUri","http://192.168.96.103/dthealth/web/MHC.PublicService.cls?wsdl");
        servletRegistrationBean.addInitParameter(ProxyServlet.P_LOG, "true");
        return servletRegistrationBean;
    }
    @Bean
    public ServletRegistrationBean servletRegistrationBean4(){
        ServletRegistrationBean servletRegistrationBean = new ServletRegistrationBean(new HisQueryProxyServlet(),"/seach/*");
        //这个setName必须要设置，并且多个的时候，名字需要不一样
        servletRegistrationBean.setName("seach");
        servletRegistrationBean.setLoadOnStartup(1);
        //QryInterface.BS.QueryService.cls
        servletRegistrationBean.addInitParameter("targetUri", "http://192.168.96.101/dthealth/web/QryInterface.BS.QueryService.cls?wsdl");
        servletRegistrationBean.addInitParameter(ProxyServlet.P_LOG, "true");

        return servletRegistrationBean;
    }

    @Bean
    public ServletRegistrationBean servletRegistrationBean(){

//        ServletRegistrationBean servletRegistrationBean =
//                new ServletRegistrationBean(new YbProxyServlet(),"/yb/*");
        ServletRegistrationBean servletRegistrationBean =
                new ServletRegistrationBean(new YbHttpProxyServlet(),"/yb/*");
        //这个setName必须要设置，并且多个的时候，名字需要不一样
        servletRegistrationBean.setName("ybserver");
//        servletRegistrationBean.addInitParameter("targetUri", "http://10.34.17.208:7003/cdytjjs/services/YtjServer");
        servletRegistrationBean.addInitParameter("targetUri", "http://10.34.17.208:8090/services/YtjServer");
        servletRegistrationBean.addInitParameter("http.read.timeout", "130000");
        servletRegistrationBean.addInitParameter("http.socket.timeout", "130000");
        servletRegistrationBean.addInitParameter("http.connectionrequest.timeout", "130000");
        servletRegistrationBean.addInitParameter(ProxyServlet.P_LOG, "true");

        return servletRegistrationBean;
    }
    @Bean
    public ServletRegistrationBean servletRegistrationBean1(){
        ServletRegistrationBean servletRegistrationBean = new ServletRegistrationBean(new CxfProxyServlet(),"/his/*");
        //这个setName必须要设置，并且多个的时候，名字需要不一样
        servletRegistrationBean.setName("hisserver");

        servletRegistrationBean.setLoadOnStartup(1);
        servletRegistrationBean.addInitParameter("targetUri", "http://192.168.96.118:57772/csp/dhc-ens/ATPayment.BS.ATPayment.cls?wsdl");
        servletRegistrationBean.addInitParameter(ProxyServlet.P_LOG, "true");

        return servletRegistrationBean;
    }
    @Override
    public void setEnvironment(Environment environment) {

    }
}
